#include "junix.h"
#include <sys/socket.h>
#include <sys/un.h>


int main(int argc, char **argv)
{
	if(argc==1)
		err_quit("Usage:%s file",argv[0] );
	unlink(argv[1]);

	struct sockaddr_un sun;
	memset(&sun,0,sizeof(sun));
	sun.sun_family=AF_UNIX;
	strcpy(sun.sun_path,argv[1]);
	int len = sizeof(sun.sun_family)+strlen(sun.sun_path);

	int sockfd=socket(AF_UNIX,SOCK_STREAM,0);
    if(sockfd==-1)
		err_sys("socket");
	if( bind(sockfd,(struct sockaddr*)&sun,len)==-1)
		err_sys("bind");

	char buf[1024];
	ssize_t sz;

	if( listen(sockfd,5)==-1)
		err_sys("listen");

	char *path="XXVV";
	int fd2=open(path,O_WRONLY|O_CREAT,0644);
	if(fd2==-1)
		err_sys("open");

	while(1)
	{
		int fd=accept(sockfd,NULL,NULL);
		if(fd==-1)
			err_sys("accept");

		while( (sz=read(fd,buf,1024))>0)
			if( write(STDOUT_FILENO, buf,sz) != sz)
				err_sys("write");

		if(sz<0)
			err_sys("read");

		if( close(fd) == -1)
			err_sys("close");

	}

	return 0;
}

